import RawaOSS from '@/tools/rawa-oss'
import { HttpRequestOptions } from 'element-ui/types/upload'

let uid = 10000

export function httpRequest (options: HttpRequestOptions) {
  const client = new RawaOSS({
    bucket: 'fs'
  } as any)

  const path = Date.now() + `-${uid++}/` + encodeURIComponent(options?.file?.name)
  client.multipartUpload(path, options.file, {
    progress: function (p, checkpoint) {
      options.onProgress({ percent: p } as any)
    }
  }).then(resp => {
    const data = {
      name: resp.name,
      url: (resp.res as any)?.requestUrls[0],
      data: resp
    }
    options.onSuccess(data)
  }).catch(e => {
    options.onError(e)
  })
}
